package com.cino.demos.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cino.annotation.AuditLog;
import com.cino.demos.entity.User;
import com.cino.demos.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    @AuditLog(operationType = AuditLog.OperationType.CREATE, title = "用户信息",
            includeFields = {"name"})
    public boolean saveUser(User user) {
        return this.save(user);
    }

    @AuditLog(operationType = AuditLog.OperationType.UPDATE,
            title = "用户信息",
            includeFields = {"name"},
            storageTable = "audit_log_customer"
    )
    @Override
    public boolean updateUser(User user) {
        return this.updateById(user);
    }


    /**
     * 自定义保存用户的目标方法
     */
    public boolean customSaveUser(User user) {
        // 在保存前添加一些自定义逻辑
        if (user.getName() == null || user.getName().trim().isEmpty()) {
            user.setName("默认用户名");
        }
        return this.save(user);
    }


    /**
     * 自定义更新用户的目标方法
     */
    public boolean customUpdateUser(User user) {
        // 在更新前添加一些自定义逻辑
        if (user.getName() != null) {
            user.setName(user.getName() + "_已更新");
        }
        return this.updateById(user);
    }

    /**
     * 测试自定义日志保存 - CREATE操作
     */
    @AuditLog(
            operationType = AuditLog.OperationType.CREATE,
            title = "用户信息",
            includeFields = {"name"},
            targetMethod = "com.cino.demos.service.CustomLogService.saveUserCreateLog"
    )
    public boolean saveUserWithCustomLog(User user) {
        return this.save(user);
    }

    /**
     * 测试自定义日志保存 - UPDATE操作
     */
    @AuditLog(
            operationType = AuditLog.OperationType.UPDATE,
            title = "用户信息",
            includeFields = {"name"},
            targetMethod = "com.cino.demos.service.CustomLogService.saveUserUpdateLog"
    )
    public boolean updateUserWithCustomLog(User user) {
        return this.updateById(user);
    }

    /**
     * 测试通用自定义日志保存
     */
    @AuditLog(
            operationType = AuditLog.OperationType.CREATE,
            title = "用户信息",
            includeFields = {"name"},
            targetMethod = "com.cino.demos.service.CustomLogService.saveCustomAuditLog"
    )
    public boolean saveUserWithGenericCustomLog(User user) {
        return this.save(user);
    }

    /**
     * 测试外部系统日志保存
     */
    @AuditLog(
            operationType = AuditLog.OperationType.UPDATE,
            title = "用户信息",
            includeFields = {"name"},
            targetMethod = "com.cino.demos.service.CustomLogService.saveToExternalSystem"
    )
    public boolean updateUserWithExternalLog(User user) {
        return this.updateById(user);
    }

    /**
     * 测试DELETE操作
     */
    @AuditLog(
            operationType = AuditLog.OperationType.DELETE,
            title = "用户信息",
            includeFields = {"name"},
            storageTable = "audit_log_customer"
    )
    public boolean deleteUser(User user) {
        return this.removeById(user.getId());
    }

    /**
     * 测试DELETE操作 - 自定义日志保存
     */
    @AuditLog(
            operationType = AuditLog.OperationType.DELETE,
            title = "用户信息",
            includeFields = {"name"},
            targetMethod = "com.cino.demos.service.CustomLogService.saveUserDeleteLog"
    )
    public boolean deleteUserWithCustomLog(User user) {
        return this.removeById(user.getId());
    }

    /**
     * 测试DELETE操作 - 自定义日志保存
     */
    @AuditLog(
            operationType = AuditLog.OperationType.DELETE,
            title = "用户信息",
            includeFields = {"name"}
    )
    @Override
    public boolean deleteUserById(Long id) {
        return this.removeById(id);
    }

}
